package Sort.sort2020_2;

import util.SwapUtil;

/**
 * 选择排序 （依次找出数组中的最小值，次小值。。。）
 * 从小到大
 * @author: leiwenkang
 * @date: 2020/5/23
 * @time: 18:17
 */
public class SelectSort {

    public static void main(String[] args) {
        int[] array = {3,8,4,6,7,2,5,9,1};
        sort(array);
        for (int i = 0; i < array.length; i++){
            System.out.print(array[i]);
            System.out.print(" ");
        }
    }

    public static void sort(int[] array) {
        if (array == null || array.length == 0) {
            return;
        }
        for (int i = 0; i < array.length; i++) {
            int min = i;
            for (int j = i + 1; j < array.length; j++) {
                if (array[j] < array[min]) {
                    min = j;
                }
            }
            //交换
            if (min != i) {
                SwapUtil.swap(array, i, min);
            }
        }

    }
}
